<?php
declare (strict_types = 1);
namespace app\user\model\user\admin;

use app\common\model\BaseModel;
use app\user\model\user\admin\Account;
use think\facade\Request;

/**
 * 定义用户余额模型
 */
class VipUser extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'user_vip';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'          => 'integer',
        'expire_time' => 'timestamp',
    ];

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';
    protected $append     = ['vip_status'];

    /**
     * 关联用户
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-30
     * @return   [type]                         [description]
     */
    public function user()
    {
        return $this->belongsTo(Account::class, 'user_id')->with(['avatar_url']);
    }

    /**
     * 获取VIP状态
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-14
     * @param    string                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getVipStatusAttr($value = '', $data)
    {
        return $data['expire_time'] > Request::time() ? 1 : 0;
    }

}
